/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nts.pointsstore;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author Himagirinathan
 */
public class ProductList extends ListActivity  implements View.OnClickListener
{
TableLayout tl;
Button bcartadd;
private DataHelper dh;
long id;
List<String[]> names;
ArrayList<String> checkeditems;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        id=this.getIntent().getExtras().getLong("id");
        this.setContentView(R.layout.productlist);
        tl=(TableLayout)this.findViewById(R.id.tablelay);
        tl.setPadding(0, 20, 0, 0);
        dh = new DataHelper(this);
        if(dh.countProducts()==0)
        {
            dh.insertProduct("iphone",100,700d);
            dh.insertProduct("ipad",150,1200d);
            dh.insertProduct("ipod",30,100d);
            dh.insertProduct("S Tab",100,1000d);
            dh.insertProduct("S Galaxy S",80,650d);
            dh.insertProduct("HTC",50,550d);
            dh.insertProduct("Galaxy 5",80,650d);
            dh.insertProduct("Galaxy Tab",80,750d);
            dh.insertProduct("Galaxy 3",80,650d);
            dh.insertProduct("HTC Magic",50,550d);
            dh.insertProduct("Galaxy Mini",80,650d);
            dh.insertProduct("HTC Desire",50,550d);
        }
        checkeditems=new ArrayList<String>(5);
        names = this.dh.selectAllProducts();
        
        TableRow row = null;
        TextView label = null;
        ImageView iv=null;
        CheckBox cb=null;
        int firstimg=R.drawable.a1;
        int i=0;
        for(String name[]:names)
        {
            row=new TableRow(this);
            iv=new ImageView(this);
            iv.setPadding(0, 0, 15, 0);
            iv.setImageResource(firstimg+(i%7));
            label=new TextView(this);
            label.setClickable(true);
            label.setText(name[1]);
            label.setHint("Select a Product to View Details");
            label.setId(Integer.parseInt(name[0]));
            label.setOnClickListener(this);
            //label.setImageResource();
            //label.setBackgroundResource(android.R.color.transparent);
            //label.setBackgroundColor(0);
            //label.setTextColor(android.R.color.white);
            //label.setGravity(Gravity.RIGHT | Gravity.TOP);
            cb=new CheckBox(this);
            cb.setId(Integer.parseInt(name[0]));
            cb.setOnClickListener(this);
            row.addView(iv);
            row.addView(label);
            row.addView(cb);
            tl.addView(row);
            i++;
        }
        row=new TableRow(this);
        row.setPadding(0, 20, 0, 0);
        row.setGravity(Gravity.CENTER_HORIZONTAL);
        bcartadd=new Button(this);
        bcartadd.setBackgroundResource(R.drawable.cartadd);
        bcartadd.setOnClickListener(this);
        row.addView(bcartadd);
        tl.addView(row);
    }

    public void onClick(View arg0)
    {
     if(arg0 instanceof CheckBox)
     {
       CheckBox cbc=(CheckBox)arg0;
       if (cbc.isChecked())
        checkeditems.add(""+cbc.getId());
       else
        checkeditems.remove(""+cbc.getId());
     }
     else if(arg0 instanceof Button)
     {
      if(checkeditems.size()==0)
      {
       AlertDialog.Builder ad=new AlertDialog.Builder(this);
       ad.setMessage("You Must Select A Product");
       ad.setNeutralButton("Ok", null);
       ad.show();
      }
      else
      {
        Intent it=new Intent(this, ConfirmChecked.class);
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("checked", checkeditems);
        bundle.putLong("id",id);
        it.putExtras(bundle);
        this.startActivity(it);
      }
     }
     else
     {
       TextView tv=(TextView)arg0;
       int productid=tv.getId();
       String cost="",points="";
       for(String name[]:names)
       {
        if(Integer.parseInt(name[0])==productid)
        {
          points=name[2];
          cost=name[3];
        }
       }

       AlertDialog.Builder ad=new AlertDialog.Builder(this);
       ad.setTitle("Product Detail");
       TableLayout tlpd=new TableLayout(this);
       tlpd.setPadding(20, 0, 0, 20);
       TableRow trpd1=new TableRow(this);
       trpd1.setPadding(0, 20, 0, 0);
       TableRow trpd2=new TableRow(this);
       trpd2.setPadding(0, 5, 0, 0);
       TableRow trpd3=new TableRow(this);
       trpd3.setPadding(0, 5, 0, 0);
       //trpd.setGravity(Gravity.CENTER_HORIZONTAL);
       TextView tvcol1=new TextView(this);
       TextView tvcol2=new TextView(this);
       TextView tvcol3=new TextView(this);
       TextView tvcol4=new TextView(this);
       TextView tvcol5=new TextView(this);
       TextView tvcol6=new TextView(this);
       tvcol1.setTextSize(16.0f);
       tvcol2.setTextSize(16.0f);
       tvcol3.setTextSize(16.0f);
       tvcol4.setTextSize(16.0f);
       tvcol5.setTextSize(16.0f);
       tvcol6.setTextSize(16.0f);
       tvcol1.setText("Product Name");
       tvcol1.setPadding(0, 0, 20, 0);
       tvcol2.setText(tv.getText().toString());
       trpd1.addView(tvcol1);
       trpd1.addView(tvcol2);
       tlpd.addView(trpd1);
       tvcol3.setText("Cost");
       tvcol3.setPadding(0, 0, 20, 0);
       tvcol4.setText(""+cost+" $ ");
       trpd2.addView(tvcol3);
       trpd2.addView(tvcol4);
       tlpd.addView(trpd2);
       tvcol5.setText("Points");
       tvcol5.setPadding(0, 0, 20, 0);
       tvcol6.setText(""+points);
       trpd3.addView(tvcol5);
       trpd3.addView(tvcol6);
       tlpd.addView(trpd3);
       ad.setView(tlpd);
       ad.setNeutralButton("Ok", null);
       ad.show();
     }
    }

}



   


